<?php
class Controller_UploadFiles extends Controller {
	
	function actionSave(){

		$dir = _gp('dir','string');
		$dir = trim($dir,".");
		
    	//上传文件
		$allowExts = 'jpg,png,gif';
		$maxSize = 2000 * 1024; // 300KB
		$uploadDir = PATH_APP_UPLOAD . $dir;
		
		$uploader = new Helper_FileUploader();
		$files =& $uploader->getFiles();
		
		/* @var $file Helper_UploadFile */
		foreach ($files as $file) {

			if (!$file->check($allowExts, $maxSize)) {
				// 上传的文件类型不符或者超过了大小限制。
				echo "ERROR:invalid upload";
				return false;
			}
		    $name = md5(time() . $file->getFilename() . $file->getSize() . $file->getTmpName());
			$filename = $name . '.' . strtolower($file->getExt());
			$file->move($uploadDir . '/' . $filename);
			
			
			$data['description'] = $file->getFilename();
			$data['size'] = $file->getSize();
			$data['dir'] = $dir;
			$data['create_by'] = UAC::getUserInfo('uid');
			$data['filename'] = $filename;
			$file_id = $this->insert($data);
			unset($data);
		}

		echo "FILEID:" . $file_id;	// Return the file id to the script

	}
	
	public function actionView(){
		$id = _gp('id','int');
		$data = $this->be($id);
		$file = PATH_APP_UPLOAD . $data['dir'] ."/" . $data['filename'];
		$image = fread(fopen($file,r),filesize($file));
		header('Content-Disposition: inline; filename='.$data['filename']);
		header("Content-type: image/jpeg");
		echo $image;
	}
	
	public function actionRemove(){

		$id = _gp('id','int');

		if(0 == $id) return;

		$data = $this->be($id);
		$file = PATH_APP_UPLOAD . $data['dir'] ."/" . $data['filename'];
		@unlink($file);
		$this->remove($id);
	}
}